#include <viewManager.h>
#include <modelConf.h>
#include <postManager.h>

namespace View {

  static gboolean
  MainWindowDeleteEvent(GtkWidget *widget,
			GdkEvent  *event,
			gpointer   user_data)
  {
    Controller::PostManager *poster = Controller::PostManager::GetPostManager();
    poster->SetBroadcastType(Controller::BROADCAST_TYPE_WRITE_BACK);
    poster->SendBroadcast();
    poster->SetBroadcastType(Controller::BROADCAST_TYPE_EXIT);
    poster->SendBroadcast();
    gtk_main_quit();
    return true;
  }

  ViewManager::ViewManager()
    :window(gtk_window_new(GTK_WINDOW_TOPLEVEL)),
     canvas(goo_canvas_new()),
     root(goo_canvas_get_root_item((GooCanvas * )canvas)),
     backgroundLayout(goo_canvas_group_new(root,NULL))
  {
    
    ViewConf *conf = ViewConf::GetViewConf();
    gdouble mainWindowLength(conf->GetDoubleData("mainWindowLength")),
      mainWindowHeigth(conf->GetDoubleData("mainWindowHeigth"));
    
    gtk_widget_set_size_request(window, mainWindowLength , mainWindowHeigth);
    gtk_window_set_resizable((GtkWindow*)window,false);
    gtk_window_set_position((GtkWindow*)window,GTK_WIN_POS_CENTER);
    g_signal_connect(G_OBJECT(window),"delete_event",
		     G_CALLBACK(MainWindowDeleteEvent),NULL);
    gtk_container_add((GtkContainer*)window,canvas);


    //background Layout  ...
    std::string confPath(VIEW_CONF_PATH );
    using namespace Model;
    ModelConf *Mconf = ModelConf::GetModelConf();
    std::string background(Mconf->GetStringData("currentBackgroundName"));
    std::string bgPath = confPath + "skin/background/" +  background ;
    GdkPixbuf *bg = gdk_pixbuf_new_from_file(bgPath.c_str() , NULL);
    backgroundImageGoo = goo_canvas_image_new(backgroundLayout,bg,0,0,NULL);
    g_object_unref(bg);
    /*
    background = "background.png";
    bgPath = confPath + "skin/background/" +  background ;
    bg = gdk_pixbuf_new_from_file(bgPath.c_str() , NULL);
    g_object_set(image,"pixbuf",bg,NULL);
    */

    //here insert the sub controll views
    SubControllView *subControllView = new SubControllView(root);
    subControllView->AddSubControllItem(Model::SYSTEM_SETTING);
    subControllView->AddSubControllItem(Model::SYN_ATTACK);
    subControllView->AddSubControllItem(Model::ARP_ATTACK);
    subControllView->AddSubControllItem(Model::SMURF_ATTACK);
    
    //dock view layout ...
    DockView *dockView = new DockView(root);
    //dockView->AddDockIcon(Model::SYSTEM_SETTING);
    //dockView->AddDockIcon(Model::SYN_ATTACK);
    //dockView->AddDockIcon(Model::ARP_ATTACK);

    subViews.push_back(subControllView);
    subViews.push_back(dockView);

    //test conf data .....
    //conf->ShowAllData();
    gtk_widget_show_all(window);
  }
  
  ViewManager::~ViewManager()
  {
    std::vector<view*>::size_type size = subViews.size();
    unsigned int i;
    for (i = 0 ; i < size ; i ++){
      delete subViews[i];
    }
    goo_canvas_item_remove(backgroundLayout);
    goo_canvas_item_remove(root);
    gtk_widget_destroy(canvas);
    gtk_widget_destroy(window);
  }

  int ViewManager::ReceiveBroadcast(Controller::Broadcast *broadcast)
  {
    std::vector<view*>::size_type size = subViews.size();
    unsigned int i;
    for (i = 0 ; i < size ; i ++){
      subViews[i]->ReceiveBroadcast(broadcast);
    }
    if(Controller::BROADCAST_TYPE_CHANGE_BACKGROUND == broadcast->GetBroadcastType()){
      std::string bgPath = VIEW_CONF_PATH + static_cast<std::string>("skin/background/") +  broadcast->GetStringData() ;
      GdkPixbuf *bg = gdk_pixbuf_new_from_file(bgPath.c_str() , NULL);
      g_object_set(backgroundImageGoo,"pixbuf",bg,(char *)NULL);
      g_object_unref(bg);
    }
    return 0;
  }

}
